package io.renren.modules.app.api;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import io.renren.common.utils.R;
import io.renren.common.utils.StringUtil;
import io.renren.common.validator.ValidatorUtils;
import io.renren.modules.app.entity.SysAppVersionVo;
import io.renren.modules.app.entity.SysParameterVo;
import io.renren.modules.app.entity.UserVo;
import io.renren.modules.app.form.CheckForm;
import io.renren.modules.app.service.ApiAppVersionService;
import io.renren.modules.app.service.ApiUserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * app检测更新api
 * @author zzh
 * @email 862583291@qq.com
 * @date 2019-4-24 12:27
 */
@Api("app检测更新api")
@RestController
@RequestMapping("/app/v1/update")
public class ApiUpdateController {
    @Autowired
    private ApiUserService userService;
    @Autowired
    private ApiAppVersionService appVersionService;

    /**
     * 检测app是否有新版本可升级
     */
    @ApiOperation(value = "检测app是否有新版本可升级")
    @RequestMapping("/checkUpdate")
    public R checkUpdate(@RequestBody CheckForm form) {
        //表单校验
        ValidatorUtils.validateEntity(form);
        //更新用户终端
        String terminal = form.getTerminal();
        if(!StringUtil.isNullOrEmpty(form.getUserId())){
            UserVo user = userService.getById(form.getUserId());
            if(!StringUtil.isNullOrEmpty(user)){
                user.setTerminal(terminal);
                user.setVersion(form.getVersion());
                if(StringUtil.isNullOrEmpty(user.getTerminals())){
                    user.setTerminals(terminal);
                }else if(user.getTerminals().indexOf(terminal) == -1){
                    user.setTerminals(user.getTerminals() + ',' + terminal);
                }
                userService.updateById(user);
            }
        }
        //检测版本
        String platform = "ios,android";
        if(platform.indexOf(terminal) != -1){
            SysAppVersionVo appVersionVo = appVersionService.getOne(
                    new QueryWrapper<SysAppVersionVo>().eq("terminal",terminal).eq("version",form.getVersion())
            );
            if(!StringUtil.isNullOrEmpty(appVersionVo) && !StringUtil.isNullOrEmpty(appVersionVo.getNextVersion())){
                SysAppVersionVo upgradeVersion = appVersionService.getOne(
                        new QueryWrapper<SysAppVersionVo>().eq("terminal",terminal).eq("version",appVersionVo.getNextVersion())
                );
                if(!StringUtil.isNullOrEmpty(upgradeVersion)){
                    return R.ok("请升级").put("data",upgradeVersion);
                }
            }
        }
        return R.ok("设置终端成功");
    }
}
